/* FILE: main.c
   DESCRIPTION: main project module */

#include <stdio.h> //for using printf, scanf
#include <stdlib.h> //for using EXIT_SUCCESS, atoi

int k = 0x45694;

/* FUNCTION: calc
   DESCRIPTION: calculate formula X = A4+C1-D4+K
   INPUT: a = A4
          d = D4
		  b = C1
   RETURN: X */
extern int calc(int a, int d, char b);

/* FUNCTION: test_calc
   DESCRIPTION: test for calc function
   INPUT: Nothing
   RETURN: Nothing */
void test_calc();

/* FUNCTION: scan
   DESCRIPTION: read data from keyboard
   INPUT: aa - A4
          bb - C1
		  dd - D4
   RETURN: Nothing */
void scan(int *aa, char *bb, int *dd);

int c_calc(int a, int d, char b);

/* FUNCTION: main
   DESCRIPTION: main project function
   INPUT: Nothing
   RETURN: EXIT_SUCCESS if program closed normally*/
int main() {
	int a, d, x, c_x;
	char b;
	scan(&a, &b, &d);
	//test_calc();
	c_x = c_calc(a,d,b);
	x = calc(a, d, b);
	printf("\n");
	system("PAUSE");
	return EXIT_SUCCESS;
}

void print(int x) {
	printf("X=%d\n", x);
}

void test_calc() {
	int a, d, x;
	char b;
	a = 5;
	b = -5;
	d = 284308;
	x = calc(a, d, b);
	if (x != 0) {
		printf("Test #1 crashed\n");
		print(x);
		system("PAUSE");
		abort();
	}
	a = 5;
	b = 1;
	d = 284315;
	x = calc(a, d, b);
	if (x != -1) {
		printf("Test #2 crashed\n");
		print(x);
		system("PAUSE");
		abort();
	}
	a = 11;
	b = 2;
	d = 11;
	x = calc(a, d, b);
	if (x != 284310) {
		printf("Test #3 crashed\n");
		print(x);
		system("PAUSE");
		abort();
	}
}

void scan(int *aa, char *bb, int *dd) {
	int a, d, temp;
	char b;

	char strInt[12];
	char strChar[5];

	printf("Expression X=A4+C1-D4+K\n");
	printf("K=0x%x\n", k);
	printf("A=");
	scanf("%s", strInt);
	a = atoi(strInt);
	printf("B=");
	scanf("%s", strChar);
	temp = atoi(strChar);
	if (temp < -128 || temp > 127) {
		printf("Error: uncorrect input data\n");
		system("PAUSE");
		abort();
	}
	b = (char) temp;
	printf("D=");
	scanf("%s", strInt);
	d = atoi(strInt);
	*aa = a;
	*bb = b;
	*dd = d;
}

int c_calc(int a, int d, char b)
{
	return a+b-d+k;
}